package org.xiaoyu.rpc.remoting.transport.cllient;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.xiaoyu.rpc.remoting.packet.AppResponse;
import org.xiaoyu.rpc.remoting.transport.cllient.future.RpcFuture;


/**
 * Created By Have
 * 2021/7/5 14:31
 */
@ChannelHandler.Sharable
@Slf4j
public class NettyClientHandler extends SimpleChannelInboundHandler<AppResponse> {
    public static final NettyClientHandler INSTANCE = new NettyClientHandler();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AppResponse msg) throws Exception {
//        try {
            log.info("客户端接收到信息:[{}]", msg);
//            ctx.channel().attr(AttributeKey.valueOf("rpcResponse" + msg.getRequestId())).set(msg);
        RpcFuture.received(ctx.channel(), msg);
//            ctx.channel().close();
//        } finally {
//            ReferenceCountUtil.release(msg);

//        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("连接出错关闭连接:", cause.getMessage(), cause);
        ChannelProvider.remove(ctx.channel());
        ctx.close();
    }


}
